package pl.rgolcz.antnet;

import java.io.IOException;
import java.io.Writer;

import edu.uci.ics.jung.graph.Graph;

/**
 * Dump the graph to provided writer (file, stdout or whatever).
 * The format is:
 * <pre>
 * n            - number of vertices (each vertex is of Integer type with the numbers (0... n-1)
 * v1 v2 weight - list of edges
 * v1 v2 weight
 * </pre>
 * @author ragg
 *
 */
public class GraphWriter
{
    private Writer writer;

    public GraphWriter(Writer writer)
    {
        this.writer = writer;
    }
    
    public void write(Graph<Integer, IWeightedEdge> graph) throws IOException
    {
        writer.write("" + graph.getVertexCount() + "\n");
        for(IWeightedEdge edge : graph.getEdges())
        {
            writer.write("" + graph.getEndpoints(edge).getFirst() 
                + " " + graph.getEndpoints(edge).getSecond()
                + " " + edge.getWeight()
                + "\n");
        }
        
        writer.close();
    }
}
